package com.ly.web.core.filter;

import com.ly.common.constant.Constants;
import com.ly.common.constant.HttpStatus;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.spring.SpringUtils;
import com.ly.framework.web.service.TokenService;
import com.ly.framework.web.service.WxLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(urlPatterns = "/wx/*", filterName = "wxAuthTokenFilter")
public class WxAuthTokenFilter implements Filter {

    private static final Logger log = LoggerFactory.getLogger(WxAuthTokenFilter.class);

    TokenService tokenService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        if(tokenService==null){
            tokenService = SpringUtils.getBean(TokenService.class);
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest httprequest = (HttpServletRequest)servletRequest;
        String openId = tokenService.getWxOpenId(httprequest);
		if( StringUtils.isEmpty(openId)){
            servletRequest.getRequestDispatcher("/403").forward(servletRequest, servletResponse);
            return;
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
